#include "chemnitz.h"

static char version [128];
static char *vers = "libchemnitz version 0";

static char *impl = "XTEA";
static const uint32_t rounds = 32;
static const uint32_t delta = (uint32_t) 0x9E3779B9;

void chemnitz_encrypt (const uint8_t *plaintextblock, const uint8_t *key, const uint8_t *ciphertextblock)
{
	uint32_t v0, v1;
	uint32_t *k, *c;
	uint32_t sum;
	uint32_t i;
	memset ((void *) ciphertextblock, 0, 8);
	sum = 0;
	v0 = ((uint32_t *) plaintextblock) [0];
	v1 = ((uint32_t *) plaintextblock) [1];
	k = (uint32_t *) key;
	c = (uint32_t *) ciphertextblock;
	for (i = 0; i < rounds; i ++)
	{
		v0 = v0 + ((v1 + ((v1 << 4) ^ (v1 >> 5))) ^ (sum + k [sum & 3]));
		sum = sum + delta;
		v1 = v1 + ((v0 + ((v0 << 4) ^ (v0 >> 5))) ^ (sum + k [(sum >> 11) & 3]));
	}
	c [0] = v0;
	c [1] = v1;
}

void chemnitz_version (char **buffer)
{
	(void) snprintf (version, 128, "%s+%s", vers, impl);
	*buffer = version;
}

